package com.bluefish.bltuan.action.admin;

import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;

import com.bluefish.bltuan.module.config.Setting;
import com.bluefish.bltuan.module.config.SettingEnum;
import com.bluefish.bltuan.service.SettingService;
import com.bluefish.bltuan.util.StringUtils;

public class SettingAction extends AdminAction {

    /**
     * 
     */
    private static final long serialVersionUID = 4012432280900980582L;

    private String[]          keys;

    private String[]          values;

    private SettingService    settingService;

    public void setSettingService(SettingService settingService) {
        this.settingService = settingService;
    }

    public void setValues(String[] values) {
        this.values = values;
    }

    public void setKeys(String[] keys) {
        this.keys = keys;
    }

    public SettingEnum[] getKeys() {
        return SettingEnum.values();
    }

    @Override
    public HttpHeaders index() {
        return new DefaultHttpHeaders(INDEX).disableCaching();
    }

    @Override
    public HttpHeaders show() {
        return null;
    }

    @Override
    public HttpHeaders create() {
        for (int i = 0; i < keys.length; i++) {
            Setting setting = this.settingService.getByKey(SettingEnum.valueOf(keys[i]));
            if (setting == null) {
                setting = new Setting();
            }
            setting.setKey(SettingEnum.valueOf(keys[i]));
            setting.setValue(values[i]);
            this.settingService.saveOrUpdate(setting);
        }
        addFlashMessage(StringUtils.UPDATE_SUCCESS);
        redirect();
        return null;
    }

    public Setting getSetting(String key) {
        return this.settingService.getByKey(SettingEnum.valueOf(key));
    }

    @Override
    public HttpHeaders editNew() {
        return null;
    }

    @Override
    public String update() {
        return null;
    }

    @Override
    public String edit() {
        return null;
    }

    @Override
    public String destroy() {
        return null;
    }

}
